// ==UserScript==
// @name         B站直播间过滤
// @namespace    http://tampermonkey.net/
// @version      0.0.2-2025-08-31
// @description  为B站直播间分区入口添加屏蔽按钮，屏蔽信息保存在LocalStorage中；页面打开后自动加载5页
// @match        https://live.bilibili.com/p/eden/area-tags?*
// @icon         https://i0.hdslb.com/bfs/static/jinkela/long/images/favicon.ico
// @grant        none
// @author       AgMonk
// @homepage     https://gitee.com/AgMonk/tampermonkey-script
// @downloadURL https://gitee.com/AgMonk/tampermonkey-script/raw/master/bilibili/bilibili-live-block.user.js
// @updateURL https://gitee.com/AgMonk/tampermonkey-script/raw/master/bilibili/bilibili-live-block.user.js
// ==/UserScript==

(function() {
    'use strict';
    //脚本配置保存的key
    const propertyKey = "live_room_block"
    //脚本配置
    var property = JSON.parse(localStorage.getItem(propertyKey))
    if (!property) property = {}

    console.log('过滤直播间辣鸡',property)

    const addRoomId = (id)=>{
        if (!property.blockId) {
            property.blockId = []
        }
        property.blockId.push(id)
        localStorage.setItem(propertyKey,JSON.stringify(property))
    }
    // 从父元素中按classname查找子元素
    const findByClass = (parent,classNames)=>{
        for (var i = 0; i < classNames.length; i++) {
            const elements = parent.getElementsByClassName(classNames[i])
            if (elements && elements.length>0){
                return [...elements]
            }
        }
    }
    // 周期性屏蔽直播间
    const intervalBlock = ()=>{
        setInterval(()=>{
            // 直播间解析
            const liveRooms = findByClass(document,[
                'index_3Uym8ODI',
                'index_item_JSGkw'
            ]).map(room=>{
                return {
                    name:findByClass(room,['Item_2GEmdhg6','Item_roomTitle_ax3eD'])[0]?.innerText?.trim(),
                    up:findByClass(room,['Item_QAOnosoB','Item_nickName_KO2QE'])[0]?.innerText?.trim(),
                    count:findByClass(room,['Item_onlineCount_FmOW6'])[0]?.innerText?.trim(),
                    element:room,
                    id:parseInt(new URL(room.getElementsByTagName('a')[0]?.href).pathname.substring(1))
                }
            })
            //console.log(liveRooms)
            liveRooms.forEach(room=>{
                // 添加屏蔽按钮
                const followButton = findByClass(room.element,['Item_3aYF6_yi','Item_follow-btn_trj3i'])[0]
                if (!followButton.bbFlag){
                    const blockButton = document.createElement('div')
                    blockButton.className = followButton.className
                    blockButton.style.backgroundColor = '#f2226d'
                    blockButton.textContent  = '屏蔽'
                    // 将屏蔽按钮插入到关注按钮后方
                    // followButton.parentNode.insertBefore(blockButton,followButton.nextSibling)
                    followButton.append(blockButton)
                    // 屏蔽按钮点击事件
                    blockButton.onclick = (event)=>{
                        event.stopPropagation();
                        if (confirm("屏蔽该直播间？")){
                            addRoomId(room.id)
                        }
                    }

                    // 按照房间号屏蔽直播间
                    if (property?.blockId?.includes(room.id)){
                        room.element.remove()
                    }
                    // 屏蔽人气小于5的
                    if (room.count && room.count<5){
                        room.element.remove()
                    }
                    followButton.bbFlag = true
                }
            })
        },3000)
    }



    setTimeout(()=>{
        // 加载页数
        const page = 5
        // 下滚加载
        for (var i=0;i<page;i++){
            setTimeout(()=>{window.scrollTo(0,999999999)},i*1000+500)
        }
        // 滚回顶部
        setTimeout(()=>{window.scrollTo(0,0)},(page+1)*1000)
        // 屏蔽直播间
        setTimeout(()=>{intervalBlock()},(page+2)*1000)
        // 自动点击关键帧
        setTimeout(()=>{
            const buttons = [...document.getElementsByClassName('tabs__normal-item')].map(e=>{
                return {
                    element:e,
                    name:e.innerText
                }
            })
            buttons[1].element.click()
        },(page+3)*1000)

    },2000)
})();